//
//  NSAttributedString+Extension.swift
//  swiftWeiBo
//
//  Created by MJT on 16/7/21.
//  Copyright © 2016年 MJT. All rights reserved.
//

import UIKit

extension NSAttributedString {
    //  根据表情模型和字体对象创建一个富文本对象
    class func attributedWithEmotion(emoticon: MJTEmoticon, font: UIFont) -> NSAttributedString {
        // 1. 根据表情图片路径名字创建一个UIImage
        let image = UIImage(named: emoticon.path!)
        // 2. 根据UIImage对象创建一个NSTextAttachment (文本附件)
        let attachtment = MJTTextAttachment()
        //  设置表情模型
        attachtment.emoticon = emoticon
        attachtment.image = image

        //  取到字体的高度
        let fontHeight = font.lineHeight
        attachtment.bounds = CGRect(x: 0, y: -4, width: fontHeight, height: fontHeight)

        // 3. 根据NSTextAttachment创建一个NSAttributedString (富文本)
        let attr = NSAttributedString(attachment: attachtment)
        return attr
    }
}
